package org.jeecg.common.util.sms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class SmsUtil {
    private static String apiKey;
    private static String secret;
    private static String sign_id;

    public static String getSign_id() {
        return sign_id;
    }

    public static void setSign_id(String sign_id) {
        SmsUtil.sign_id = sign_id;
    }

    public static String getApiKey() {
        return apiKey;
    }

    public static void setApiKey(String apiKey) {
        SmsUtil.apiKey = apiKey;
    }

    public static String getSecret() {
        return secret;
    }

    public static void setSecret(String secret) {
        SmsUtil.secret = secret;
    }

    public static boolean sendSms(String mobile, String content) throws IOException {
        boolean result = false;
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("https://api.4321.sh/sms/send");
        httpPost.addHeader("Content-Type","application/json");
        Map<String,Object> map = new HashMap<>();
        map.put("apikey", apiKey);
        map.put("secret", secret);
        map.put("sign_id", sign_id);
        map.put("mobile", mobile);
        map.put("content", content);
        String json = JSON.toJSONString(map);
        httpPost.setEntity(new StringEntity(json,"UTF-8"));
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String res = EntityUtils.toString(entity);
        log.info("---------------短信接口返回的数据----------------");
        log.info("res == {}", res);
        JSONObject object = JSON.parseObject(res);
//        log.info("{Code:" + sendSmsResponse.getCode()+",Message:" + sendSmsResponse.getMessage()+",RequestId:"+ sendSmsResponse.getRequestId()+",BizId:"+sendSmsResponse.getBizId()+"}");
//        String ok = "OK";
        if (0 == Integer.valueOf(object.get("code").toString())) {
            result = true;
        }
        return result;
    }


    public static boolean sendSmsByTemplate(String mobile) throws Exception{
        boolean result = false;
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("https://api.4321.sh/sms/template");
        httpPost.addHeader("Content-Type","application/json");
        Map<String,Object> map = new HashMap<>();
        map.put("apikey", apiKey);
//        map.put("apikey", "N1330799b69");
        map.put("secret", secret);
//        map.put("secret", "13307943024bf6eda");
        map.put("sign_id", 238667);
        map.put("template_id", 276426);
        map.put("mobile", mobile);
        map.put("content", "");
        String json = JSON.toJSONString(map);
        httpPost.setEntity(new StringEntity(json,"UTF-8"));
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String res = EntityUtils.toString(entity);
        log.info("---------------短信接口返回的数据----------------");
        log.info("res == {}", res);
        JSONObject object = JSON.parseObject(res);
//        log.info("{Code:" + sendSmsResponse.getCode()+",Message:" + sendSmsResponse.getMessage()+",RequestId:"+ sendSmsResponse.getRequestId()+",BizId:"+sendSmsResponse.getBizId()+"}");
//        String ok = "OK";
        if (0 == Integer.valueOf(object.get("code").toString())) {
            result = true;
        }
        return result;
    }

    public static void main(String[] args) {
        try {
            sendSmsByTemplate("15900675459");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
